package com.licq.ldoc.parser.impl;

import java.lang.annotation.Annotation;

import com.licq.ldoc.domain.Element;
import com.licq.ldoc.domain.ElementType;
import com.licq.ldoc.domain.Occurrencies;

/**
 * 
 * @author liujian
 *
 */
public final class ElementParser extends AbstractElementParser<Element> {
	
	private Class<?> clazz;
	
    private ElementParser(Class<?> clazz) {
    	resetCurrentStatus();
    	getCurrentStatus().setTopClazz(clazz);
    	this.clazz = clazz;
	}
    
    public static Element parse(Class<?> clazz) {
		return new ElementParser(clazz).doParse();
    }

    public Element doParse() {
    	int currentLevel = getCurrentLevel();
    	Element element = new Element(currentLevel, clazz.getSimpleName(), ElementType.type(clazz),
    			Occurrencies.ONLY_ONCE, true);
    	recordClazzToHandled(clazz, element);
    	parseAnnotations(element);
    	parseFields(clazz, element);
    	return element;
    }
    
    /**
     * deal with the clazz's annotations
     * @param clazz
     * @param element
     */
    private void parseAnnotations(Element element) {
		Annotation[] annotations = clazz.getAnnotations();
		AnnotationParsees.parse(element, annotations);
	}
    
}

